package cn.com.shopec.erp.system.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cn.com.shopec.erp.base.entity.SupplierInfo;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.MD5Util;
import cn.com.shopec.erp.core.base.controller.BaseController;
import cn.com.shopec.erp.system.common.UserUtils;
import cn.com.shopec.erp.system.constant.SystemConstant;
import cn.com.shopec.erp.system.entity.User;
import cn.com.shopec.erp.system.service.IRoleService;
import cn.com.shopec.erp.system.service.IUserService;
import cn.com.shopec.erp.system.vo.RoleVo;
import cn.com.shopec.erp.system.vo.UserVo;

/**
 * 
 * @ClassName UserController
 * @Description 用户Controller
 * @author chengui
 * @date 2013-5-30 上午10:55:11
 */
@Controller
@RequestMapping("/manager/system/user")
public class UserController extends BaseController {
	
	@Resource private IUserService userService;
	@Resource private IRoleService roleService;
	
	/**
	 * 
	 * @MethodName userList
	 * @Description 供应链用户列表
	 * @author chengui
	 * @date 2013-5-30 上午11:15:59
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("userList")
	public ModelAndView userList(UserVo userVo, ModelMap modelMap) throws Exception {
		modelMap.put("userVo", userVo);
		modelMap.put("uv", UserUtils.getUserVo());
		return new ModelAndView("system/user/user_list");
	}
	
	/**
	 * 
	 * @MethodName ajaxToUserList
	 * @Description ajax分页查询用户列表
	 * @author chengui
	 * @date 2013-5-30 上午11:46:33
	 * @param userVo
	 * @param pageQuery
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("ajaxToUserList")
	@ResponseBody
	public String ajaxToUserList(UserVo userVo, PageQuery pageQuery) throws Exception {
		PageFinder<UserVo> pageFinder = userService.getPagedUserVos(userVo, pageQuery);
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("Rows", pageFinder.getData());
		jsonObj.put("Total", pageFinder.getRowCount());
		return jsonObj.toJSONString();
	}
	
	/**
	 * 
	 * @MethodName addUser
	 * @Description 新增供应链用户页面
	 * @author chengui
	 * @date 2013-5-30 下午1:57:42
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("toAddUser")
	public ModelAndView toAddUser() throws Exception {
		return new ModelAndView("system/user/add_user");
	}
	
	/**
	 * 
	 * @MethodName addUser
	 * @Description 新增供应链用户
	 * @author chengui
	 * @date 2013-5-30 下午2:11:39
	 * @param userVo
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping("addUser")
	@ResponseBody
	public String addUser(UserVo userVo) throws Exception {
		userService.addUser(userVo);
		return SystemConstant.SUCCESS;
	}
	
	/**
	 * 
	 * @MethodName isUserExist
	 * @Description 检查用户是否已经存在
	 * @author chengui
	 * @date 2013-5-30 下午2:35:10
	 * @param userVo
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping("isUserExist")
	@ResponseBody
	public String isUserExist(UserVo userVo) throws Exception {
		String result = userService.isUserExist(userVo);
		return result;
	}
	
	/**
	 * 
	 * @MethodName toModifyUser
	 * @Description 修改用户页面
	 * @author chengui
	 * @date 2013-5-30 下午3:14:12
	 * @param userVo
	 * @param modelMap
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("toModifyUser")
	public ModelAndView toModifyUser(UserVo userVo, ModelMap modelMap) throws Exception {
		UserVo uv = userService.getUserVo(userVo);
		modelMap.put("userVo", uv);
		return new ModelAndView("system/user/modify_user");
	}
	
	/**
	 * 
	 * @MethodName modifyUser
	 * @Description 修改用户
	 * @author chengui
	 * @date 2013-5-30 下午3:15:30
	 * @param userVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("modifyUser")
	@ResponseBody
	public String modifyUser(UserVo userVo) throws Exception {
		userService.modifyUser(userVo);
		return SystemConstant.SUCCESS;
	}
	
	/**
	 * 
	 * @MethodName toModifyPassword
	 * @Description 修改密码页面
	 * @author chengui
	 * @date 2013-5-30 下午3:42:22
	 * @param userVo
	 * @param modelMap
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("toModifyPassword")
	public ModelAndView toModifyPassword(UserVo userVo, ModelMap modelMap) throws Exception {
		UserVo uv = userService.getUserVo(userVo);
		modelMap.put("userVo", uv);
		return new ModelAndView("system/user/modify_user_pwd");
	}
	
	/**
	 * 
	 * @MethodName toAllocateRole
	 * @Description 分配角色页面
	 * @author chengui
	 * @date 2013-5-30 下午7:59:12
	 * @param userVo
	 * @param modelMap
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("toAllocateRole")
	public ModelAndView toAllocateRole(UserVo userVo, ModelMap modelMap) throws Exception {
		List<RoleVo> ownRoleVos = userService.getOwnRoleVos(userVo);
		List<RoleVo> roleVos = roleService.getAllRoleVo();
		List<RoleVo> leftRoleVos = userService.getLeftRoleVos(roleVos, ownRoleVos);
		modelMap.put("userVo", userVo);
		modelMap.put("ownRoleVos", ownRoleVos);
		modelMap.put("leftRoleVos", leftRoleVos);
		return new ModelAndView("system/user/allocate_role");
	}
	
	/**
	 * 
	 * @MethodName allocateRole
	 * @Description 分配角色
	 * @author chengui
	 * @date 2013-5-31 上午9:29:50
	 * @param userVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("allocateRole")
	@ResponseBody
	public String allocateRole(UserVo userVo) throws Exception {
		userService.allocateRole(userVo);
		return SystemConstant.SUCCESS;
	}
	
	/**
	 * 
	 * @MethodName modifyPassword
	 * @Description 修改密码
	 * @author chengui
	 * @date 2013-5-30 下午3:44:06
	 * @param userVo
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("modifyPassword")
	@ResponseBody
	public String modifyPassword(UserVo userVo) throws Exception {
		String result = userService.modifyPassword(userVo);
		return result;
	}
	
	/**
	 * 
	 * @MethodName lockUser
	 * @Description 锁定用户
	 * @author chengui
	 * @date 2013-5-30 下午3:57:59
	 * @param userVo
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping("lockUser")
	@ResponseBody
	public String lockUser(UserVo userVo) throws Exception {
		userService.lockUser(userVo);
		return SystemConstant.SUCCESS;
	}
	
	/**
	 * 
	 * @MethodName unlockUser
	 * @Description 解锁用户
	 * @author chengui
	 * @date 2013-5-30 下午4:03:41
	 * @param userVo
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping("unlockUser")
	@ResponseBody
	public String unlockUser(UserVo userVo) throws Exception {
		userService.unlockUser(userVo);
		return SystemConstant.SUCCESS;
	}
	
	/**
	 * 
	 * @MethodName deleteUser
	 * @Description 删除用户
	 * @author chengui
	 * @date 2013-5-30 下午4:05:34
	 * @param userVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("deleteUser")
	@ResponseBody
	public String deleteUser(UserVo userVo) throws Exception {
		userService.deleteUser(userVo);
		return SystemConstant.SUCCESS;
	}
	
    /**
     * 
     * @Title: validationIsExistOldPwd
     * @Description: 验证旧密码是否输出有误
     * @date: 2013-6-15下午03:40:50
     */
    @RequestMapping("checkPwd")
    @ResponseBody
    public String checkPwd(String id, String oldPwd) {
        try {
            String pass = MD5Util.MD5Encode(oldPwd, "");
            User user = this.userService.getUserById(id);
            if (LogicUtil.isNotNull(user)) {
                if (!user.getPwd().equals(pass)) {
                    return SZSCMConstant.SUCCESS;
                }
            }
        } catch (Exception e) {
            return SZSCMConstant.FAIL;
        }

        return SZSCMConstant.FAIL;
    }
	
}
